#ifndef _COMMON_TYPE_H_
#define _COMMON_TYPE_H_
#include <unistd.h>
#include <time.h>
#include "../logclient.h"

#ifndef uint64
#define uint64 unsigned long long		
#endif
#ifndef uint32
#define uint32 unsigned int		
#endif
#ifndef uint16
#define uint16 unsigned short		
#endif
#ifndef uint8
#define uint8 unsigned char		
#endif

#ifndef sint64
#define sint64 long long		
#endif
#ifndef sint32
#define sint32 int		
#endif
#ifndef sint16
#define sint16 short
#endif

#ifndef sint8
#define sint8 char
#endif


// from linux source
#define SWAP_16(x) \
((uint16)( \
    (((uint16)(x) & (uint16)0x00ffU) << 8) | \
    (((uint16)(x) & (uint16)0xff00U) >> 8) ))
#define SWAP_32(x) \
((uint32)( \
    (((uint32)(x) & (uint32)0x000000ffUL) << 24) | \
    (((uint32)(x) & (uint32)0x0000ff00UL) << 8) | \
    (((uint32)(x) & (uint32)0x00ff0000UL) >> 8) | \
    (((uint32)(x) & (uint32)0xff000000UL) >> 24) ))

#if HAL_SUPPORT_INT64
#define SWAP_64(x) \
((uint64)( \
    (uint64)(((uint64)(x) & C_UINT64(0x00000000000000ff)) << 56) | \
    (uint64)(((uint64)(x) & C_UINT64(0x000000000000ff00)) << 40) | \
    (uint64)(((uint64)(x) & C_UINT64(0x0000000000ff0000)) << 24) | \
    (uint64)(((uint64)(x) & C_UINT64(0x00000000ff000000)) << 8) | \
    (uint64)(((uint64)(x) & C_UINT64(0x000000ff00000000)) >> 8) | \
    (uint64)(((uint64)(x) & C_UINT64(0x0000ff0000000000)) >> 24) | \
    (uint64)(((uint64)(x) & C_UINT64(0x00ff000000000000)) >> 40) | \
    (uint64)(((uint64)(x) & C_UINT64(0xff00000000000000)) >> 56) ))
#endif

#if IS_BIG_ENDIAN
#if HAL_SUPPORT_INT64
#define SWAP_64_ON_BE(x)                    SWAP_64(x)
#define SWAP_64_ON_LE(x)                    (x)
#endif
#define SWAP_32_ON_BE(x)                    SWAP_32(x)
#define SWAP_16_ON_BE(x)                    SWAP_16(x)
#define SWAP_32_ON_LE(x)                    (x)
#define SWQP_16_ON_LE(x)                    (x)
#else
#if HAL_SUPPORT_INT64
#define SWAP_64_ON_BE(x)                    (x)
#define SWAP_64_ON_LE(x)                    SWAP_64(x)
#endif
#define SWAP_32_ON_BE(x)                    (x)
#define SWAP_16_ON_BE(x)                    (x)
#define SWAP_32_ON_LE(x)                    SWAP_32(x)
#define SWAP_16_ON_LE(x)                    SWAP_16(x)
#endif

extern FILE* fd_logfile;
extern void SOCK_printf(const   char   *form ,...);

#ifndef fun_printf
typedef void (*fun_printf)(const   char   *form ,...);
typedef int (*fun_RxChar)(char *pt_Chr);
typedef void (*fun_TxChar)(char chr);
#endif

extern fun_printf gp_printf;
extern const char* __MODULE__;
#define loglen (64*1024)

/*
#define LOG_DBG(f, ...)\
do{\
	char logbuf[loglen+1];\
	snprintf(logbuf,(loglen), f, ##__VA_ARGS__);\
	logbuf[loglen] = 0;\
	printf("[File:%s(%d)]:%s", __FILE__, __LINE__, logbuf);\
} while(0)
*/

#define LOG_DBG(f, ...)\
do{\
	char logbuf[loglen+1];\
	char slogbuf[loglen+1];\
	snprintf(logbuf,(loglen), f, ##__VA_ARGS__);\
	logbuf[loglen] = 0;\
	sprintf(slogbuf, "[File:%s(%d)]:%s", __FILE__, __LINE__, logbuf);\
	printf("%s", slogbuf);\
	LogInst::instance().Send(eLogRate_debug, slogbuf);  \
} while(0)

#define LOG_PRINTF(f, ...)                                         \
do {                                                            \
    gp_printf (f, ##__VA_ARGS__);          \
    if(fd_logfile) \
        fprintf(fd_logfile, f, ##__VA_ARGS__);          \
} while(0)

#define LOG_TRACE()                                         \
do {                                                            \
    gp_printf ("%s:%d\n", __FUNCTION__, __LINE__ );          \
} while(0)

typedef enum _data_buf_info_size
{
    SIZE_32B = 0,
    SIZE_64B,
    SIZE_128B,
    SIZE_256B,
    SIZE_512B,
    SIZE_1KB,
    SIZE_2KB,

    SIZE_NUM
}DATA_BUF_INFO_SIZE;

#define DATA_BUF_LEN_32B    32
#define DATA_BUF_LEN_64B    64
#define DATA_BUF_LEN_128B    128
#define DATA_BUF_LEN_256B    256
#define DATA_BUF_LEN_512B    512
#define DATA_BUF_LEN_1KB    1024
#define DATA_BUF_LEN_2KB    2048
typedef struct _data_buf_info_32b {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_32B];
} DATA_BUF_INFO_SIZE_32B;
typedef struct _data_buf_info_64b {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_64B];
} DATA_BUF_INFO_SIZE_64B;
typedef struct _data_buf_info_128b {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_128B];
} DATA_BUF_INFO_SIZE_128B;
typedef struct _data_buf_info_256b {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_256B];
} DATA_BUF_INFO_SIZE_256B;
typedef struct _data_buf_info_512b {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_512B];
} DATA_BUF_INFO_SIZE_512B;
typedef struct _data_buf_info_1kb {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_1KB];
} DATA_BUF_INFO_SIZE_1KB;
typedef struct _data_buf_info_2kb {
    uint16  size_type;
    uint16  item;
    sint8    buf[DATA_BUF_LEN_2KB];
} DATA_BUF_INFO_SIZE_2KB;


typedef struct _data_buf_info {
    uint16  size_type;
    uint16  item;
    union{
        sint8    buf[DATA_BUF_LEN_32B];
        sint8    buf_2k[DATA_BUF_LEN_2KB];
    }data;
} DATA_BUF_INFO;

typedef struct _buf_status {
    int buf_used_max;
    int buf_used;
    int buf_num;
} BUF_STATUS;

#endif /* _COMMON_TYPE_H_ */

